package com.yumijinfu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yumijinfu.R;
import com.yumijinfu.base.BaseActivity;
import com.yumijinfu.http.HttpCallBack;
import com.yumijinfu.http.HttpUtils;
import com.yumijinfu.mian.Config;
import com.yumijinfu.tool.VibratorUtil;
import com.yumijinfu.util.DES;
import com.yumijinfu.view.ClearEditText;
import com.yumijinfu.view.TitleBar;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 登录密码
 */
public class LoginPasswordActivity extends BaseActivity {

    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.et_old_password)
    ClearEditText etOldPassword;
    @Bind(R.id.et_new_password)
    ClearEditText etNewPassword;
    @Bind(R.id.et_new_confirm_password)
    ClearEditText etNewConfirmPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login_password);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {

        titlebar.setTitleBar(R.drawable.back_selector, "修改登录密码", 0);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {

    }

    @OnClick({R.id.titlebar_iv_back, R.id.tv_password_renzheng})
    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.titlebar_iv_back:

                finish();

                break;


            case R.id.tv_password_renzheng:
                long[] pattern = { 100, 400, 100, 400 };
                String OldPassword = etOldPassword.getText().toString().trim();
                String NewPassword = etNewPassword.getText().toString().trim();
                String NewConfirmPassword = etNewConfirmPassword.getText().toString().trim();
                if (TextUtils.isEmpty(OldPassword)) {
                    etOldPassword.setShakeAnimation();
                    VibratorUtil.Vibrate(LoginPasswordActivity.this,pattern,false);
                    Toast.makeText(LoginPasswordActivity.this, "请输入原始密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(NewPassword)) {
                    etNewPassword.setShakeAnimation();
                    VibratorUtil.Vibrate(LoginPasswordActivity.this,pattern,false);
                    Toast.makeText(LoginPasswordActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(NewConfirmPassword)) {

                    etNewConfirmPassword.setShakeAnimation();
                    VibratorUtil.Vibrate(LoginPasswordActivity.this,pattern,false);
                    Toast.makeText(LoginPasswordActivity.this, "请输入确定密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!NewPassword.equals(NewConfirmPassword)) {
                    VibratorUtil.Vibrate(LoginPasswordActivity.this,pattern,false);
                    etNewConfirmPassword.setShakeAnimation();
                    etNewPassword.setShakeAnimation();
                    Toast.makeText(LoginPasswordActivity.this, "新密码与确认密码不匹配,请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                } else {


                    getsaveLoginPassword();


                }

                break;


            default:
        }
    }

    /**
     *
     * 修改登录密码
     */
    private void getsaveLoginPassword() {
        HttpParams params=new HttpParams();
        try {
            params.put("userOldPassword", DES.encryptDES(etOldPassword.getText().toString().trim(),Config.DES_KEY));
            params.put("userPassword",DES.encryptDES(etNewPassword.getText().toString().trim(),Config.DES_KEY));
            params.put("userPassword2",DES.encryptDES(etNewConfirmPassword.getText().toString().trim(),Config.DES_KEY));
            HttpUtils.post(Config.SAVERESETPASSWORD, params, new HttpCallBack(this,"加载中...") {
                @Override
                public void onSucceed(JSONObject result, String code, String message) {

                    Toast.makeText(LoginPasswordActivity.this, message, Toast.LENGTH_SHORT).show();
                    //重新登录
                    startActivity(new Intent(LoginPasswordActivity.this,LoginActivity.class));
                    finish();
                }

                @Override
                public void onError(String code, String message) {
                    Toast.makeText(LoginPasswordActivity.this, message, Toast.LENGTH_SHORT).show();

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
